import cn.weiyang.biz.AccountBiz;
import cn.weiyang.biz.RecordBiz;
import cn.weiyang.model.Account;
import cn.weiyang.model.Record;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017-7-15.
 */
public class Demo {
    ApplicationContext applicationContext=null;
    @Before
    public void init(){
        applicationContext=new ClassPathXmlApplicationContext("spring.xml");
    }

    /**
     * 开户
     */
    @Test
    public void test1(){
        AccountBiz accountBiz= (AccountBiz) applicationContext.getBean("accountBiz");
        Account account=new Account();
        account.setUser_name("杨志");
        account.setMoney(42522f);
        account.setCreatedate(new Date());
        accountBiz.addAccount(account);
    }

    /**
     * 查询所有账户信息
     */
    @Test
    public void test2(){
        AccountBiz accountBiz= (AccountBiz) applicationContext.getBean("accountBiz");
        String hql="from Account";
        List<Account> accounts=accountBiz.findAllAccount(hql);
        for (Account account:accounts){
            System.out.println(account.getUser_name()+" "+account.getMoney()+" "+account.getCreatedate());
            System.out.println(account.getUser_name()+","+account.getFormrecords().size());
        }


    }
    /**
     * 根据交易号获取交易记录
     */
    @Test
    public void tes3(){
        RecordBiz recordBiz= (RecordBiz) applicationContext.getBean("recordBiz");
        Record record=recordBiz.get(4);
        System.out.println(record.getFromaccount().getUser_name()+" "+record.getToaccount().getUser_name()+" "+record.getRecord_date());
    }
    /**
     * 查询所有交易记录
     */
    @Test
    public void test4(){
        RecordBiz recordBiz= (RecordBiz) applicationContext.getBean("recordBiz");
        String hql="from Record";
        List<Record> records=recordBiz.findAllRecord(hql);
        for(Record record:records){
            System.out.println(record.getFromaccount().getUser_name()+" "+record.getToaccount().getUser_name()+" "+record.getRecord_date());
        }
    }

    /**
     * 测试转账
     */
    @Test
    public void test5(){
        AccountBiz accountBiz= (AccountBiz) applicationContext.getBean("accountBiz");
        /*获得转出账户*/
        Account fromaccount=accountBiz.get(5);
        /*获得转入账户*/
        Account toaccount=accountBiz.get(6);
        accountBiz.doTransferAccount(fromaccount,toaccount,134567f);
    }

}
